

package com.mt.mall.controller.Order;

import com.mt.common.core.web.JsonResult;
import com.mt.mall.entity.Order.Order;
import com.mt.mall.entity.User.ShippingAddress;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.Order.OrderEditDto;
import com.mt.mall.service.Order.OrderService;
import com.mt.mall.service.good.CommodityService;
import com.mt.common.system.service.UserService;
import com.mt.mall.service.merchant.ShopService;


@Api(tags = "订单")
@RestController
@RequestMapping("/api/mall/Order/Order")
@CrossOrigin(allowCredentials = "true")
public class OrderController {
    private static Logger logger = LoggerFactory.getLogger(OrderController.class);


    @Autowired
    private OrderService orderService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;

    /**
     * 根据分页参数查询订单集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("分页查询订单")
    @ApiPageParam
    @PostMapping("/findOrders")
    public PageResultDTO findOrders(@RequestBody PageDTO pageDTO) {
        return this.orderService.findOrders(pageDTO);
    }

    /**
     * 根据ID查询指定的订单
     *
     * @param orderId Id
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("根据id查询订单")
    @ApiPageParam
    @PostMapping("/findOrder")
    public Order findOrder(@RequestParam Long orderId) {
        return this.orderService.findOrder(orderId);
    }

    /**
     * 根据ID查询指定的订单(包含外键名称)
     *
     * @param orderId Id
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("根据ID查询指定的订单(包含外键名称)")
    @PostMapping("/findOrderForView")
    public Order findOrderForView(@RequestParam Long orderId) {
        return this.orderService.findOrderWithForeignName(orderId);
    }

    /**
     * 根据ID查询指定的订单(包含订单和外键名称)
     *
     * @param orderId Id
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("根据ID查询指定的订单(包含订单和外键名称)")
    @PostMapping("/findOrderForEdit")
    public OrderEditDto findOrderForEdit(@RequestParam Long orderId) {
        OrderEditDto orderEditDto = new OrderEditDto();
        orderEditDto.setOrder(this.orderService.findOrderWithForeignName(orderId));

        this.prepareOrderEditDto(orderEditDto);

        return orderEditDto;
    }

    /**
     * 根据ID查询指定的订单(只提取ID 和 Name)
     *
     * @param orderId Id
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("根据ID查询指定的订单(只提取ID 和 Name)")
    @PostMapping("/findOrdersWithIdNameById")
    public Order findOrdersWithIdNameById(@RequestParam Long orderId) {
        return this.orderService.findOrdersWithIdNameById(orderId);
    }

    /**
     * 根据名称查询订单集合(只提取ID 和 Name)
     *
     * @param orderName 名称
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:view')")
    @ApiOperation("根据名称查询订单集合(只提取ID 和 Name)")
    @PostMapping("/findOrdersWithIdNameByName")
    public List<Order> findOrdersWithIdNameByName(@RequestParam String orderName) {
        return this.orderService.findOrdersWithIdNameByName(orderName);
    }


    /**
     * 创建新的订单
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:add')")
    @ApiOperation("创建新的订单")
    @PostMapping("/createOrder")
    public OrderEditDto createOrder() {
        OrderEditDto orderEditDto = new OrderEditDto();
        orderEditDto.setOrder(new Order());

        this.prepareOrderEditDto(orderEditDto);
        return orderEditDto;
    }

    private void prepareOrderEditDto(OrderEditDto orderEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        orderEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        orderEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        orderEditDto.setShopShops(this.shopService.findAllShopsWithIdName());
    }

    /**
     * 新增保存订单
     *
     * @param order 实体对象
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:add')")
    @ApiOperation("新增保存订单")
    @PostMapping("/saveOrder")
    public Order saveOrder(@RequestBody Order order) {
        return this.orderService.saveOrder(order);
    }

    /**
     * 修改保存订单
     *
     * @param order 实体对象
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:update')")
    @ApiOperation("修改保存订单")
    @PostMapping("/updateOrder")
    public Order updateOrder(@RequestBody Order order) {
        return this.orderService.updateOrder(order);
    }

    @ApiOperation("修改订单状态")
    @PostMapping("/updateOrderStatus")
    public JsonResult updateOrderStatus(@Param("orderId")Long orderId,@Param("status")String status) {
        return this.orderService.updateOrderStatus(orderId,status);
    }

    /**
     * 根据ID删除订单
     *
     * @param orderId ID
     */
    @PreAuthorize("hasAuthority('mall:Order:Order:remove')")
    @ApiOperation("根据ID删除订单")
    @PostMapping("/deleteOrder")
    public void deleteOrder(@RequestParam Long orderId) {
        this.orderService.deleteOrder(orderId);
    }


    @ApiOperation("用户提交订单")
    @PostMapping("/putOrder")
    public JsonResult putOrder(@RequestBody Order order) {
        return this.orderService.putOrder(order);
    }


    @ApiOperation("用户支付订单")
    @PostMapping("/payOrder")
    public JsonResult payOrder(@Param("orderId") Long orderId) {
        return this.orderService.payOrder(orderId);
    }

    @ApiOperation("用户确认收获")
    @PostMapping("/confirmReceipt")
    public JsonResult confirmReceipt(@Param("orderId") Long orderId) {
        return this.orderService.confirmReceipt(orderId);
    }

    @ApiOperation("用户修改地址")
    @PostMapping("/changeAddress")
    public JsonResult changeAddress(@RequestBody Order order) {
        return this.orderService.changeAddress(order);
    }

    @PostMapping("/delivery")
    public JsonResult delivery(@Param("orderId")Long orderId) {
        return this.orderService.delivery(orderId);
    }
}

